package com.wwg.threads.binghe.concurrent.executor;

import java.util.concurrent.*;

public class FutureTaskTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(() -> "futureTask 测试 返回");
        // 结合现成使用
//        executeByThread(futureTask);

        executeByThreadPool(futureTask);

    }

    private static void executeByThreadPool(FutureTask<String> futureTask) throws InterruptedException, ExecutionException {
        // 结合线程池使用
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(futureTask);
        System.out.println(futureTask.get());
        executorService.shutdown();
    }

    private static void executeByThread(FutureTask<String> futureTask) throws InterruptedException, ExecutionException {
        new Thread(futureTask).start();
        System.out.println(futureTask.get());
    }
}
